package com.dunqi.gpm.shunqing.home.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.common.view.RecyclerItemDecoration;
import com.dunqi.gpm.shunqing.home.activity.YearPlanDetailActivity;
import com.dunqi.gpm.shunqing.home.adapter.YearPlanAdapter;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.bean.Project;
import com.dunqi.gpm.shunqing.home.bean.YearPlan;
import com.dunqi.gpm.shunqing.home.constance.Constance;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/11/26.
 */

public class YearPlanListFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    private Project project;
    int page = 1;
    ArrayList<YearPlan> data = new ArrayList<>();
    private YearPlanAdapter adapter;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .queryProjectAnnualPlanList(project.getProjectId(), page)
                .compose(RxSchedulers.<Page<YearPlan>>io_main())
                .subscribe(new Consumer<Page<YearPlan>>() {
                    @Override
                    public void accept(Page<YearPlan> yearPlanPage) throws Exception {

                        ArrayList<YearPlan> list = yearPlanPage.getList();
                        boolean hasNextPage = yearPlanPage.isHasNextPage();

                        if (page == 1) {
                            data.clear();
                        }
                        data.addAll(list);
                        adapter.notifyDataSetChanged();
                        page++;
                        SwipeUtil.loadCompleted(swipeRefresh);

                        if (!hasNextPage) {
                            adapter.loadMoreEnd(true);
                            return;
                        } else {
                            adapter.loadMoreComplete();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);
                        adapter.loadMoreFail();
                    }
                });
    }

    @Override
    protected void initUiAndListener() {
        recycler.setLayoutManager(new LinearLayoutManager(context));
        adapter = new YearPlanAdapter(data);
        recycler.setAdapter(adapter);

        recycler.addItemDecoration(new RecyclerItemDecoration(1, RecyclerItemDecoration.VERTICAL));

        swipeRefresh.setOnRefreshListener(this);

        adapter.setOnLoadMoreListener(this, recycler);

        adapter.setOnItemClickListener(this);
        adapter.setEmptyView(new EmptyView(context));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_year_plan;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        project = (Project) arguments.getSerializable(Constance.PROJECT);
    }

    public static YearPlanListFragment newInstance(Project project) {
        YearPlanListFragment fragment = new YearPlanListFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constance.PROJECT, project);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        YearPlan yearPlan = data.get(position);
        YearPlanDetailActivity.start(context, yearPlan.getPlanYear(), yearPlan.getProjectId());
    }
}